package com.p2p.front.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.p2p.base.page.PageVo;
import com.p2p.front.dal.dao.LoanInfoDao;
import com.p2p.front.dal.dao.UserInfoDao;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.LoanPhoto;
import com.p2p.front.dal.model.LoanVariation;
import com.p2p.front.service.LoanInfoService;
import com.p2p.front.service.LoanPhotoService;
import com.p2p.front.service.LoanVariationService;
import com.p2p.front.web.form.LoanForm;

@Service("loanInfoService")
public class LoanInfoServiceImpl implements LoanInfoService {

	@Resource
	private LoanInfoDao loanInfoDao;

	@Resource
	private UserInfoDao userInfoDao;

	@Resource
	private LoanVariationService loanVariationService;

	@Resource
	private LoanPhotoService loanPhotoService;

	@Override
	public LoanInfo getLoanInfoById(Long id) {
		return loanInfoDao.getLoanInfoById(id);
	}

	@Deprecated
	public Map<String, Object> get(Long id) {
		Map<String, Object> params = loanInfoDao.get(id);
		return params;
	}

	@Override
	public PageVo list(PageVo form) {
		List<LoanInfo> list = loanInfoDao.listPageBackend(form);
		form.setList(list);
		return form;
	}

	@Override
	public PageInfo listPageByStatus(LoanForm form) {
		PageHelper.startPage(form.getPageNum(), form.getRows());
		List<LoanInfo> list = loanInfoDao.listPageByStatus(form);
		PageInfo page = new PageInfo(list);
		return page;
	}

	@Override
	public boolean updateStatus(Long lid, int status) {
		LoanInfo loanInfo = loanInfoDao.getLoanInfoById(lid);
		if (loanInfo != null) {
			if (status == 32) {
				return 1 == loanInfoDao.updateForTenderEnd(lid, status,
						loanInfo.getStatus(), new Date());
			} else {
				return 1 == loanInfoDao.updateStatus(lid, status,
						loanInfo.getStatus());
			}

		}
		return false;
	}

	@Override
	public boolean updateStatus(Long lid, int status, int oldStatus) {
		LoanInfo loanInfo = loanInfoDao.getLoanInfoById(lid);
		if (loanInfo != null) {
			if (status == 32) {
				return 1 == loanInfoDao.updateForTenderEnd(lid, status,
						oldStatus, new Date());
			} else {
				return 1 == loanInfoDao.updateStatus(lid, status, oldStatus);
			}

		}
		return false;
	}

	@Override
	public void saveLoanInfo(LoanInfo loanInfo) {
		loanInfoDao.saveLoanInfo(loanInfo);
	}

	// 暂时废弃了
	@Deprecated
	public List<LoanInfo> list(Integer count) {
		return loanInfoDao.list(count);
	}

	@Override
	public List<Map<String, Object>> batchList(List list) {
		return loanInfoDao.batchList(list);
	}

	@Override
	public void updateLoanInfo(LoanInfo loanInfo) {
		// TODO Auto-generated method stub
		loanInfoDao.updateLoanInfo(loanInfo);
	}

	@Override
	public List<LoanInfo> findByStatus(List<Integer> statusList) {
		// TODO Auto-generated method stub
		return loanInfoDao.findByStatus(statusList);
	}

	@Override
	public boolean updateAuto(Long loanId, Integer auto, Integer oldAuto) {
		// TODO Auto-generated method stub
		int updateAuto = loanInfoDao.updateAuto(loanId, auto, oldAuto);
		return 1 == updateAuto;
	}

	@Override
	public boolean updateBeginEndTime(Long loanId, Date begintime, Date endtime) {
		return 1 == loanInfoDao.updateBeginEndTime(loanId, begintime, endtime);

	}

	@Override
	public Long getDayAmount(Date dateBegin, Date dateEnd) {
		// TODO Auto-generated method stub
		return loanInfoDao.getDayAmount(dateBegin, dateEnd);
	}

	@Override
	public void updateDesc(Long lid, String description) {
		// TODO Auto-generated method stub
		loanInfoDao.updateDesc(lid, description);
	}

	// 改动了，不再仅仅是查询成功的标
	@Override
	public List<LoanInfo> getLastSucceededLoan() {
		List<LoanInfo> list = (List<LoanInfo>) loanInfoDao
				.getLastSucceededLoan();
		List<Long> loanIds = new ArrayList<Long>();
		List<Long> loanIdsForPic = new ArrayList<Long>();
		for (LoanInfo loanInfo : list) {
			loanIds.add(loanInfo.getLid());
			loanIdsForPic.add(loanInfo.getLid());
			if (loanInfo.getPlid() != null && loanInfo.getPlid() > 0) {
				loanIdsForPic.add(loanInfo.getPlid());
			}
		}
		if (loanIds.size() > 0) {
			Map<Long, LoanVariation> loanVariationMap = loanVariationService
					.findLoanVariationByIds(loanIds);
			Map<Long, List<LoanPhoto>> loanPhotoMap = loanPhotoService
					.findByLidListAndType(loanIdsForPic, 1);
			// List<LoanPhoto> photos = null;
			for (LoanInfo loanInfo : list) {
				loanInfo.setLoanVariation(loanVariationMap.get(loanInfo
						.getLid()));

				loanInfo.setLoanPhotoList(loanPhotoMap.get(loanInfo.getLid()));
				if (loanInfo.getPlid() > 0) {
					List<LoanPhoto> plidPhotos = loanPhotoMap.get(loanInfo
							.getPlid());
					if (plidPhotos != null && plidPhotos.size() > 0) {
						List<LoanPhoto> lidPhotos = loanInfo.getLoanPhotoList();
						if (lidPhotos == null || lidPhotos.size() == 0) {
							loanInfo.setLoanPhotoList(plidPhotos);
						} else {
							lidPhotos.addAll(plidPhotos);
						}
					}

				}
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> batchWaitList() {
		return loanInfoDao.getAllWait();
	}

	@Override
	public Map<Long, LoanInfo> findByIdList(List<Long> lidList) {
		List<LoanInfo> list = loanInfoDao.findByIdList(lidList);
		Map<Long, LoanInfo> map = new HashMap<Long, LoanInfo>();
		for (LoanInfo loanInfo : list) {
			map.put(loanInfo.getLid(), loanInfo);
		}
		return map;
	}

	@Override
	public LoanInfo findTopLoan() {
		return loanInfoDao.getTop();
	}

}
